package com.a0soft.gphone.aTruffleHog.wnd.phone;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.gio.ui.BaseActivity;
import com.a0soft.gphone.aTruffleHog.scrsht.Screenshot;
import com.a0soft.gphone.aTruffleHog.util.CONSTS;
import com.a0soft.gphone.aTruffleHog.util.aDebug;
import com.a0soft.gphone.aTruffleHog.wnd.stat.BaseViewPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.DefaultStatsPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.FindDevicesByCPUFormFrg;
import com.a0soft.gphone.aTruffleHog.wnd.stat.FindDevicesByCPUPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.FindDevicesByCameraFormFrg;
import com.a0soft.gphone.aTruffleHog.wnd.stat.FindDevicesByCameraPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.OsStatsPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.RootedStatsPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.StatsListFrg;
import com.a0soft.gphone.aTruffleHog.wnd.stat.TopDeviceStatsPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.TopOperatorStatsPager;
import com.jakewharton.android.viewpagerindicator.TitlePageIndicator;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

public class StatDetailWnd extends BaseActivity {
    BaseViewPager m_pager;
    
    private static final String LOG_TAG = StatDetailWnd.class.getSimpleName();
    
    ////////////////////////////////////////////
    @Override public void 
    onCreate(Bundle bdSavedInstanceState) 
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onCreate()");

        super.onCreate(bdSavedInstanceState);
        
        setContentView(R.layout.stats_wnd);
        
        getActivityHelper().setupActionBar(getTitle(), 0);
        
        if (!DeviceInfo.GetInstance(this).IsReady()) {
            finish();
            return;
        }
        
        _Init();
    }
    
    @Override public void 
    onBackPressed()
    {
        if (m_pager != null) {
            if (m_pager instanceof FindDevicesByCameraPager) {
                if (((FindDevicesByCameraPager)m_pager).BackToFirstPage())
                    return;
            }
            else if (m_pager instanceof FindDevicesByCPUPager) {
                if (((FindDevicesByCPUPager)m_pager).BackToFirstPage())
                    return;
            }
        }
        super.onBackPressed();
    }

    private void
    _Init() 
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "_Init()");
        
        Intent it = getIntent();
        int nSelItemPos = it.getIntExtra(StatsListFrg.EXTRA_SEL_ITEM_POS, Integer.MIN_VALUE);
        boolean bSelItemChanged = it.getBooleanExtra(StatsListFrg.EXTRA_SEL_ITEM_CHANGED, false);    
        if (aDebug.ENABLED) {
            aDebug.d(LOG_TAG, "  nSelItemPos=" + nSelItemPos);
            aDebug.d(LOG_TAG, "  bSelItemChanged=" + bSelItemChanged);
        }
        
        BaseViewPager pager;
        
        switch (nSelItemPos) {
        case StatsListFrg.ITEM_OS_STATS:
            pager = new OsStatsPager(); break;
        case StatsListFrg.ITEM_ROOTED_STATS:
            pager = new RootedStatsPager(); break;
        case StatsListFrg.ITEM_TOP_DEVICE_STATS:
            pager = new TopDeviceStatsPager(); break;
        case StatsListFrg.ITEM_TOP_OPERATOR_STATS:
            pager = new TopOperatorStatsPager(); break;
        case StatsListFrg.ITEM_FIND_BY_CAMERA:
            pager = new FindDevicesByCameraPager(); break;
        case StatsListFrg.ITEM_FIND_BY_CPU:
            pager = new FindDevicesByCPUPager(); break;
        default:
            pager = new DefaultStatsPager(); break;
        }
        
        m_pager = pager;
        
        pager.SetupViews(this, 
                         (ViewPager)findViewById(R.id.pager), 
                         (TitlePageIndicator)findViewById(R.id.indicator));
    }

    private int
    _GetStripColor(int nIdx)
    {
        return CONSTS.PIE_COLORS[nIdx % CONSTS.PIE_COLORS.length];    
    }
    
    @Override protected void 
    onPostCreate(Bundle bdSavedInstanceState) 
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onPostCreate()");
        super.onPostCreate(bdSavedInstanceState);
        
        getActivityHelper().setupSubActivity();
        
        String[] ITEMS_TITLE = getResources().getStringArray(R.array.stat_items_title);

        Intent it = getIntent();
        int nSelItemPos = it.getIntExtra(StatsListFrg.EXTRA_SEL_ITEM_POS, Integer.MIN_VALUE);
        getActivityHelper().setActionBarTitle(ITEMS_TITLE[nSelItemPos]);
        getActivityHelper().setActionBarColor(_GetStripColor(nSelItemPos));
    }
    
    @Override public boolean
    onCreateOptionsMenu(Menu menu) 
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onCreateOptionsMenu()");
        getMenuInflater().inflate(R.menu.stats_wnd, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override public boolean 
    onOptionsItemSelected(MenuItem item)
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onOptionsItemSelected()");
        final int ID = item.getItemId();
        if (ID == R.id.menu_screenshot) {
            Screenshot.TakeScreenshot(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override public void 
    onNotifyByFragment(Class<?> clsFrg, Intent it)
    {
        if (aDebug.ENABLED) {
            aDebug.d(LOG_TAG, "onNotifyByFragment()");
        }
        if (it == null) return;
        
        if (clsFrg.equals(FindDevicesByCameraFormFrg.class)) {
            int nMinPixels = it.getIntExtra(FindDevicesByCameraFormFrg.EXTRA_CAMERA_PIXELS_MIN, -1);
            int nMaxPixels = it.getIntExtra(FindDevicesByCameraFormFrg.EXTRA_CAMERA_PIXELS_MAX, -1);
            if (aDebug.ENABLED) {
                aDebug.d(LOG_TAG, "  nMinPixels=" + nMinPixels + ", nMaxPixels=" + nMaxPixels);
            }    
            
            if (m_pager != null && m_pager instanceof FindDevicesByCameraPager) {
                ((FindDevicesByCameraPager)m_pager).FindDevices(this, nMinPixels, nMaxPixels);
            }
        }
        else if (clsFrg.equals(FindDevicesByCPUFormFrg.class)) {
            int nMinCoreNum = it.getIntExtra(FindDevicesByCPUFormFrg.EXTRA_CORE_NUM_MIN, -1);
            int nMaxCoreNum = it.getIntExtra(FindDevicesByCPUFormFrg.EXTRA_CORE_NUM_MAX, -1);
            int nMinCpuSpeed = it.getIntExtra(FindDevicesByCPUFormFrg.EXTRA_CPU_SPEED_MIN, -1);
            int nMaxCpuSpeed = it.getIntExtra(FindDevicesByCPUFormFrg.EXTRA_CPU_SPEED_MAX, -1);
            if (aDebug.ENABLED) {
                aDebug.d(LOG_TAG, "  nMinCoreNum=" + nMinCoreNum + ", nMaxCoreNum=" + nMaxCoreNum);
                aDebug.d(LOG_TAG, "  nMinCpuSpeed=" + nMinCpuSpeed + ", nMaxCpuSpeed=" + nMaxCpuSpeed);
            }    
            
            if (m_pager != null && m_pager instanceof FindDevicesByCPUPager) {
                ((FindDevicesByCPUPager)m_pager).FindDevices(this, 
                                                             nMinCoreNum, nMaxCoreNum, 
                                                             nMinCpuSpeed, nMaxCpuSpeed);
            }
        }
    }
}
